package ClientCommands;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.json.simple.JSONObject;

import Annotations.IsClientCommand;
import Commands.ClientCommandBase;
import Commands.ExecuteMethod;
import DomainLayer.FeedBackEntity;

import Helpers.ApplicationLogger;
import Presenters.JSONPresenter;
import ServiceLayer.ServiceFacade;

@IsClientCommand
public class GetFeedBack extends ClientCommandBase<List<JSONPresenter>>	
{
	private class FeedBackJSONPresenter implements JSONPresenter	
	{
		public FeedBackJSONPresenter(FeedBackEntity feedback)
		{
			if (feedback != null)
			{
				setId(feedback.getId());
				setMessage(feedback.getMessage());
				setFrom(feedback.getFrom());
				setDate(feedback.getDate());
				setPage(feedback.getPage());
			}
		}
		
		private Integer id;
		private String message;
		private String from;
		private BigDecimal date;
		private String page;
		
		public Integer getId() {
			return id;
		}
		public void setId(Integer id) {
			this.id = id;
		}

		public String getMessage() {
			return message;
		}
		public void setMessage(String message) {
			this.message = message;
		}

		public String getFrom() {
			return from;
		}
		public void setFrom(String from) {
			this.from = from;
		}

		public BigDecimal getDate() {
			return date;
		}
		public void setDate(BigDecimal date) {
			this.date = date;
		}

		public String getPage() {
			return page;
		}
		public void setPage(String page) {
			this.page = page;
		}
		
		@Override
		public String toJSONString() {
			StringBuffer sb = new StringBuffer();                                  
			sb.append("{");                      
			sb.append("\"" + JSONObject.escape("Id") + "\"");                 
			sb.append(":");                 
			sb.append("\"" + JSONObject.escape(getId().toString()) + "\"");                                  
			sb.append(",");                                  
			sb.append("\"" + JSONObject.escape("Message") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(getMessage()) + "\"");
			sb.append(",");
			sb.append("\"" + JSONObject.escape("From") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(getFrom()) + "\"");
			sb.append(",");
			sb.append("\"" + JSONObject.escape("Date") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(String.valueOf(getDate())) + "\"");
			sb.append(",");
			sb.append("\"" + JSONObject.escape("Page") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(getPage()) + "\"");
			sb.append("}");                      
			return sb.toString();
		}
	}
	
	public GetFeedBack()
	{
		setCommandName("GetFeedBack");
		setCommandDescription("Get feedback");
		setExecuteMethod(ExecuteMethod.ServerOnly);
	}
	
	@Override
	protected void ExecuteInternal() throws IOException 
	{
		List<JSONPresenter> feedbackList = new ArrayList<JSONPresenter>();
		try	{
			ServiceLayer.ServiceFacade sf = new ServiceFacade();
			List<FeedBackEntity> feedback = (List<FeedBackEntity>)sf.GetFeedBack();
			for (FeedBackEntity feedbackEntity : feedback)
			{
				feedbackList.add(new FeedBackJSONPresenter(feedbackEntity));
			}
		}catch (Throwable e) 
		{ ApplicationLogger.getInstance().LOG.error(e.getMessage()); }
	}
	
	public void FillFromJSON(JSONObject jObj) {
		super.FillFromJSON(jObj);
	}
	
	public String toJSONString()
	{
		StringBuffer sb = new StringBuffer();                                  
		sb.append("{");                        
		sb.append("\"" + JSONObject.escape("CommandName") + "\"");                 
		sb.append(":");                 
		sb.append("\"" + JSONObject.escape(getCommandName()) + "\"");                                  
		sb.append(",");                                  
		sb.append("\"" + JSONObject.escape("CommandDescription") + "\"");                 
		sb.append(":");                 
		sb.append("\"" + getCommandDescription() + "\"");   
		sb.append(",");                                  
		sb.append("\"" + JSONObject.escape("ExecuteMethod") + "\"");    
		sb.append(":");
		sb.append("\"" + getExecuteMethod() + "\"");
		sb.append("}");                                  
		return sb.toString();
	}
}
